﻿using Microsoft.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.Identity;

namespace EcoSystem.EntityFrameworkCore;

[ConnectionStringName(EcoSystemDbProperties.ConnectionStringName)]
public class EcoSystemDbContext : AbpDbContext<EcoSystemDbContext>, IEcoSystemDbContext
{
    /* Add DbSet for each Aggregate Root here. Example:
     * public DbSet<Question> Questions { get; set; }
     */

    /** 配置相关 **/
    public DbSet<ConfigCategory> ConfigCategory { get; set; }
    public DbSet<ConfigDefine> ConfigDefine { get; set; }
    public DbSet<ConfigSetting> ConfigSetting { get; set; }
    public DbSet<TakeConfig> TakeConfigs { get; set; }
    public DbSet<TakeNumber> TakeNumbers { get; set; }

    /** 数据字典 **/
    public DbSet<DictionaryItem> DictionaryItems { get; set; }
    public DbSet<DictionaryValue> DictionaryValues { get; set; }

    /** 用户及权限 */
    public DbSet<SystemUser> SystemUsers { get; set; }
    public DbSet<SystemMenu> SystemMenus { get; set; }
    public DbSet<DataPermission> DataPermissions { get; set; }
    public DbSet<DataPermissionGrant> DataPermissionGrands { get; set; }

    /** 文件管理相关 */
    public DbSet<Bucket> Buckets { get; set; }
    public DbSet<DirResource> DirResources { get; set; }
    public DbSet<FileResource> FileResources { get; set; }

    /** 行政区划及组织架构 */
    public DbSet<Region> Regions { get; set; }
    public DbSet<Enterprise> Enterprises { get; set; }
    public DbSet<Organization> Organizations { get; set; }

    /** 附件管理相关 **/
    public DbSet<AttachmentConfig> AttachmentConfigs { get; set; }
    public DbSet<Attachment> Attachments { get; set; }

    /** 报表相关 **/
    public DbSet<ReportCategoryConfig> ReportCategories { get; set; }
    public DbSet<HttpApiConfig> HttpApiConfigs { get; set; }
    public DbSet<ReportConfig> ReportConfigs { get; set; } 

    /** 页面通知信息 **/
    public DbSet<Notification> Notifications { get; set; }

    public EcoSystemDbContext(DbContextOptions<EcoSystemDbContext> options)
        : base(options)
    {

    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.EnableSensitiveDataLogging();
        base.OnConfiguring(optionsBuilder);
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        //系统用户
        builder.Entity<SystemUser>(b =>
        {
            b.ToTable(AbpCommonDbProperties.DbTablePrefix + "Users", EcoSystemDbProperties.DbSchema);
            b.ConfigureByConvention();
        });

        base.OnModelCreating(builder);
        builder.ConfigureEcoSystem();
    }
}
